﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace DreamExplanation
{
    public partial class SearchControl : UserControl
    {
        
        public SearchControl()
        {
            InitializeComponent();

            acbSearchClass.ItemsSource = App.keyWrodList;
        }

        
        private void btSearch_Click(object sender, RoutedEventArgs e)
        {
            if (App.navigationInfos != null
                && App.navigationInfos.Count() > 0
                && !string.IsNullOrEmpty(acbSearchClass.Text))
            {
                string searchText = acbSearchClass.Text.Trim(); 
                List<NavigationInfo> selectedContents = (from o in App.navigationInfos
                                                    where o.Content.Contains(searchText)
                                                    select o).ToList();

                List<string> subcategories = (from o in App.navigationInfos
                                                            where o.SubCategoryName.Contains(searchText)
                                                            select o.SubCategoryName).ToList();
                if (App.contentKeyWrodList == null)
                {
                    App.contentKeyWrodList = new List<string>();
                }
                else
                {
                    App.contentKeyWrodList.Clear();
                }
                //add sub category
                if (subcategories != null
                    && subcategories.Count() > 0)
                {
                    App.contentKeyWrodList.AddRange(subcategories);
                }
                else
                {
                    ShowNoRecord();
                }

                if (selectedContents != null
                    && selectedContents.Count() > 0)
                {
                    //generate search content list for count  =20
                    int count = 20;
                    int i = 0;
                    foreach (NavigationInfo navigationInfo in selectedContents)
                    {
                        if (i < count)
                        {
                            if (!App.contentKeyWrodList.Contains(navigationInfo.SubCategoryName))
                            {
                                App.contentKeyWrodList.Add(navigationInfo.SubCategoryName);
                                i = i + 1;
                            }
                        }
                        else
                        {
                            break;
                        }

                    }
                    HideNoRecord();

                    string uri = string.Format("/SubCategoryPage.xaml?categoryName={0}&&subCategoryName={1}&&contentSearch={2}", "", "", searchText);

                    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri(uri, UriKind.Relative));
                }
                else
                {
                    ShowNoRecord();
                }
                
            }
        }

        public void ShowNoRecord()
        {
            noRecord.Visibility = System.Windows.Visibility.Visible;
        }

        public void HideNoRecord()
        {
            noRecord.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
